﻿#include "debugdialog.h"
#include "ui_debugdialog.h"
#include "mainwindow.h"

#include <QMessageBox>

DebugDialog::DebugDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DebugDialog)
{
    ui->setupUi(this);

    m_TcpSocket = new TcpSocket(this);

    ui->textBrowser->setStyleSheet(QString::fromUtf8 ("background-color:black"));

    connect(m_TcpSocket, SIGNAL(debugsocketmessage(TcpSocket*,QString)), this, SLOT(onprocessdebugsocketmessage(TcpSocket*,QString)));
}

DebugDialog::~DebugDialog()
{
    delete ui;

    delete m_TcpSocket;
}

void DebugDialog::connectDebugServer(void)
{
    if(MainWindow::getSingleton().getCurrentClient() == NULL)
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("当前没有选中服务器!"));
        return;
    }

    m_TcpSocket->connectTcpServer(MainWindow::getSingleton().getCurrentClient()->ip,
                                  MainWindow::getSingleton().getCurrentClient()->debugport);
}

void DebugDialog::disconnectDebugServer(void)
{
    m_TcpSocket->close();
}

void DebugDialog::onprocessdebugsocketmessage(TcpSocket *psocket,QString message)
{
    ui->textBrowser->append("<font color=white>" + message + "</font>");
}

void DebugDialog::on_pushButton_clicked()
{
    if(ui->lineEdit->text().isEmpty())
        return;

    m_TcpSocket->send(ui->lineEdit->text());
    //ui->lineEdit->setText("");
}
